﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TrackClasses;

namespace WinFormsTrackDesigner
{
    public partial class FormDesigner : Form
    {
        public FormDesigner()
        {
            InitializeComponent();
            //_r = new Road();
            _r = Road.GetTestRoad();
        }

        private Road _r = null;
        private void pnTrack_Paint(object sender, PaintEventArgs e)
        {
            Pen p = new Pen(Brushes.Green);
            p.Width = 5;
            for (int i = 0; i < _r.RoadParts.Length - 1; i++)
            {
                RoadPart rp1 = _r.RoadParts[i];
                RoadPart rp2 = _r.RoadParts[i + 1];
                e.Graphics.DrawLine(p, rp1.X1, rp1.Y1, rp2.X1, rp2.Y1);
                e.Graphics.DrawLine(p, rp1.X2, rp1.Y2, rp2.X2, rp2.Y2);
            }

            if (rpNew != null)
            {
                if (_r.GetFinishRoadPart() != null)
                    e.Graphics.DrawLine(p, _r.GetFinishRoadPart().X1, _r.GetFinishRoadPart().Y1, rpNew.X1, rpNew.Y1);
                else
                    e.Graphics.DrawLine(p, rpNew.X1 + 1, rpNew.Y1 + 1, rpNew.X1, rpNew.Y1);
            }

        }

        private RoadPart rpNew = null;
        private void pnTrack_MouseClick(object sender, MouseEventArgs e)
        {
            if (rpNew == null)
            {
                rpNew = new RoadPart();
                rpNew.X1 = e.X;
                rpNew.Y1 = e.Y;
            }
            else
            {
                rpNew.X2 = e.X;
                rpNew.Y2 = e.Y;
               
                
                _r.AddRoadPart(rpNew);
                tbCode.Text += string.Format("r.AddRoadPart(new RoadPart({0}, {1}, {2}, {3}));", rpNew.X1, rpNew.Y1, rpNew.X2, rpNew.Y2);
                tbCode.Text += Environment.NewLine;

                rpNew = null;
            }
            pnTrack.Invalidate();
        }

        private void btnUndo_Click(object sender, EventArgs e)
        {
            if (rpNew == null)
                _r.RemoveLastRoadPart();
            else
                rpNew = null;
            pnTrack.Invalidate();
        }
    }
}
